package net.keebook.webapp.bean;

import java.io.IOException;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import org.apache.log4j.Logger;

import net.keebook.webapp.dao.GroupDao;
import net.keebook.webapp.dao.KeeBookDao;
import net.keebook.webapp.entity.ItemGroup;
import net.keebook.webapp.entity.KeeBook;
import net.keebook.webapp.model.SessionModel;
import net.keebook.webapp.util.AccessRight;
import net.keebook.webapp.util.Messages;


@ManagedBean
@RequestScoped
public class AddGroupBean {
	private String groupId;
	private String title;
	@Inject
	private GroupDao groupDao;
	@Inject
	private KeeBookDao keeBookDao;
	private SessionModel sessionModel;
	
	@PostConstruct
	public void init() throws IOException {
		setSessionModel(SessionModel.getInstance(FacesContext.getCurrentInstance()));
		
		if (!getKeeBookDao().getKeeBookAccess(getSessionModel(), AccessRight.READ_WRITE)) {
			Logger.getLogger(this.getClass()).warn("Forbidden access to bean from userId="+getSessionModel().getUserId());
			FacesContext.getCurrentInstance().getExternalContext().redirect("403.xhtml");
			return;
		}
		
		if (FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().containsKey("id")) {
			String id = new String(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id"));
			ItemGroup group = getGroupDao().get(id);
			if (group == null || !getGroupDao().canAccess(getSessionModel().getUserId(), group.getId())) {
				Logger.getLogger(this.getClass()).warn("Forbidden access to bean from userId="+getSessionModel().getUserId());
				FacesContext.getCurrentInstance().getExternalContext().redirect("403.xhtml");
				return;
			}
			setGroupId(group.getExposableId());
			setTitle(group.getTitle());
		}
	}
	
	public void save() throws IOException {
		if (!getKeeBookDao().getKeeBookAccess(getSessionModel(), AccessRight.READ_WRITE)) {
			Logger.getLogger(this.getClass()).warn("Forbidden access to bean from userId="+getSessionModel().getUserId());
			FacesContext.getCurrentInstance().getExternalContext().redirect("403.xhtml");
			return;
		}
		
		KeeBook keeBook = getKeeBookDao().get(getSessionModel().getKeeBookId());
		ItemGroup group = new ItemGroup();
		group.setTitle(getTitle());
		group.setKeeBook(keeBook);
		getGroupDao().store(group);
		FacesContext.getCurrentInstance().getExternalContext().redirect("editgroup.xhtml?id="+group.getExposableId()+"&msg="+Messages.ITEM_CREATED);
	}
	
	public void update() throws IOException {
		if (!getKeeBookDao().getKeeBookAccess(getSessionModel(), AccessRight.READ_WRITE)) {
			Logger.getLogger(this.getClass()).warn("Forbidden access to bean from userId="+getSessionModel().getUserId());
			FacesContext.getCurrentInstance().getExternalContext().redirect("403.xhtml");
			return;
		}
		
		ItemGroup group = getGroupDao().get(getGroupId());
		group.setTitle(getTitle());
		getGroupDao().update(group);
		FacesContext.getCurrentInstance().getExternalContext().redirect("editgroup.xhtml?id="+group.getExposableId()+"&msg="+Messages.ITEM_UPDATED);
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public GroupDao getGroupDao() {
		return groupDao;
	}

	public void setGroupDao(GroupDao groupDao) {
		this.groupDao = groupDao;
	}

	public KeeBookDao getKeeBookDao() {
		return keeBookDao;
	}

	public void setKeeBookDao(KeeBookDao keeBookDao) {
		this.keeBookDao = keeBookDao;
	}

	public SessionModel getSessionModel() {
		return sessionModel;
	}

	public void setSessionModel(SessionModel sessionModel) {
		this.sessionModel = sessionModel;
	}
}
